package cn.dnf.user.controller;

import cn.dnf.basic.exception.BusinessException;
import cn.dnf.basic.util.JsonResult;
import cn.dnf.user.dto.LoginDto;
import cn.dnf.user.dto.RegisterDto;
import cn.dnf.user.dto.WxDto;
import cn.dnf.user.service.ILogininfoService;
import cn.dnf.user.service.IloginService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private IloginService loginService;

    @PostMapping("/account")
    public JsonResult account(@RequestBody LoginDto dto){
        try {
            //service层,直接返回一个JsonResult
            Map<String,Object> map = loginService.account(dto);
            //返回一个map给JsonResult
            return JsonResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙!");
        }
    }

    /**
     * 微信登录
     * @param
     * @return
     */
    @GetMapping("/weChat/{code}")
    public JsonResult weChat(@PathVariable ("code") String code){
        try {
            //让service直接返回一个jsonResult
             return loginService.weChat(code);

        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙!");
        }
    }

    @PostMapping("/wechat/binder")
    public JsonResult binder(@RequestBody WxDto dto){
        try {
            //让service直接返回一个jsonResult

            return loginService.binder(dto);

        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙!");
        }
    }

    /*@PostMapping("/phoneRegister")
    public JsonResult phoneRegister(@RequestBody RegisterDto dto){
        try {
            UserService.phoneRegister(dto);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络异常,稍后重试");
        }

    }*/
}
